// ElSimOct13_vs10.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include "Elevator.h"
#include "Clock.h"
#define ELEVATOR_COUNT 2

int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;
	//TODO: uncomment this when code debugging is finished.
	//srand(time(NULL)); 
	//***Initialize Program***
	int simCount = 1;						// Number of simulation runs
	int elevatorCount = 2;					// Number of elevators
	bool saveRunData = false;				// flag to control saving individual run data
	string response = "yes";				// Saves input responses
	string fileNameBase;					// Used to generate file names





	//Get Test Parameters
	cout << "Do you want to save data from each run?(yes or no) ";
	getline(cin, response);
	if (response.compare(0,3,"yes")== 0 || response.compare(0,3,"Yes")== 0) saveRunData = true;
	cout << endl << "What name would you like for the base filename? ";
	getline(cin, fileNameBase);
	cout << endl << "How many elevators? ";
	cin >> elevatorCount;
	cout << endl << "How many simulation runs? ";
	cin >> simCount;

	// Complete the initialization
	TicketHandler Tickets(elevatorCount, fileNameBase, saveRunData);	// Create Ticket Handler
	vector <Ticket*> newPassengers;			// Create TicketHolder
	vector <Floor> Floors(5);				// Create Floors
	vector <Elevator> Elevators;			// Create Elevators

	// Create each elevator

	for(int i= 0; i < elevatorCount; i++)
	{
		Elevators.push_back(Elevator(&Floors, &Tickets, i));
	}

	//***Main Loop***
	for (int mLoopCount = 0; mLoopCount < simCount; mLoopCount++)
	{
		// Generate simulation data
		Tickets.createData(mLoopCount);
		// Initialize Floors
		for ( auto& F: Floors)
		{
			F.init();
		}
		// Initialize Elevators
		for (auto& E: Elevators)
		{
			E.init();
		}
		Clock::reset();
		//***Simulation Loop***
		while ( (!Tickets.simOver() ) && (Clock::getTime() < 45000) )
		{
			// Get tickets
			Tickets.getTickets(newPassengers,Clock::getTime());
			// Move tickes to floor queues
			for (auto passenger : newPassengers)
			{
				Floors[passenger->getStartFloor()].loadQ(passenger);
			}
			newPassengers.clear();
			// Update simulation
			for (auto& E : Elevators)
			{
				E.update(Clock::getTime());
			}
			// Increment time
			Clock::timeTick();

		}
		//***Analyze run data***
		// Process tickets, Calculate statistics, Save data to a file if flag set
		Tickets.RunDataAnalysis(mLoopCount);
	}
	// Store combined data
	Tickets.writeFinalAnalysis();

	cout << "Simulation complete." << endl;
	system("pause");
	return 0;
}

